<template>
    <div class="addAdult">
        <div class="add_item" @click="outerVisible = true">创建正式班</div>
        <!-- 创建体验班 -->
        <div class="dialog_from">
            <el-dialog title="创建正式班" :modal=false :close-on-click-modal=false :visible.sync="outerVisible">
                <el-form :model="form">
                    <el-form-item label="班级名称：" :label-width="formLabelWidth">
                        <el-input v-model="form.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="销售：" :label-width="formLabelWidth">
                        <el-select v-model="form.saleid">
                            <el-option v-for='(sale,i) in saleList'
                            :key="i"
                            :label="sale.realname"
                            :value="sale.userid"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="线下中教：" :label-width="formLabelWidth">
                        <el-select v-model="form.assitant">
                            <el-option v-for='(assitant,j) in assitantList'
                            :key="j"
                            :label="assitant.realname"
                            :value="assitant.userid"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="班级备注：" :label-width="formLabelWidth">
                        <el-input v-model="form.remark" auto-complete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button class="dialog_close" @click="outerVisible = false">取 消</el-button>
                    <el-button class="dialog_confirm" type="primary" @click="creatClass">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>
<script>
import base from '../../api/base.js'
export default {
    data() {
        return {
                assitantList:'',
                saleList:'',
                outerVisible: false,
                form: {
                    name: '',
                    delivery: false,
                    name:'',
                    assitant:'',
                    saleid:'',
                    remark:'',
                },
                outerVisible: false,
                // 表格
                formLabelWidth: '160px',
                columWidth:140,
                align:'center',
        };
    },
    methods:{
        creatClass(){
            
            this.$axios({
                url: base.baseUrl+"/school/class/set-class",
                method: "POST",
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                data:{
                    token:localStorage.token,
                    name:this.form.name,
                    assistant:this.form.assitant,
                    saleid:this.form.saleid,
                    remark:this.form.remark,
                    classtype:2
                },
                transformRequest: [function (data) {
                    let ret = ''
                    for (let it in data) {
                        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                    }
                    return ret
                }],
            }).then(res => {
                if(res.data.code==-1){
                    this.alertTip(res.data.msg)
                }else if(res.data.code==200){
                    this.alertTip('创建成功')
                    this.outerVisible = false
                    this.$emit('refresh')
                }
            });
        },
        getSaleList(){
            this.$axios({
                url: base.baseUrl+"/school/agent/get-role-name",
                method: "POST",
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                data:{
                    token:localStorage.token,
                    role:18
                },
                transformRequest: [function (data) {
                    let ret = ''
                    for (let it in data) {
                        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                    }
                    return ret
                }],
            }).then(res => {
                if(res.data.code==200){
                    this.saleList=res.data.data
                }else if(res.data.code==-2){
                    this.$router.push('/login');
                }
                
            });
        },
        getAssistantList(){
            this.$axios({
                url: base.baseUrl+"/school/agent/get-role-name",
                method: "POST",
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                data:{
                    token:localStorage.token,
                    role:2
                },
                transformRequest: [function (data) {
                    let ret = ''
                    for (let it in data) {
                        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                    }
                    return ret
                }],
            }).then(res => {
                if(res.data.code==200){
                    this.assitantList=res.data.data
                }else if(res.data.code==-2){
                    this.$router.push('/login');
                }
                
            });
        },
        alertTip(msg){
            this.$alert(msg, '提示', {
                confirmButtonText: '确定',
                callback: action => {
                    this.$message({
                        type: 'info',
                    });
                }
            });
        }
    },
    created(){
        this.getSaleList()
        this.getAssistantList()
    }
};
</script>

